
#include "manual_manipulation.h"
#include "ui_manualmanipulation.h"

ManualManipulation::ManualManipulation(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ManualManipulation)
{
    ui->setupUi(this);

    setTable(ui->table_axisExpress);
    initializeBackground();
    initializeButtons();
    updateMantuableData();

    connectSignalsAndSlots();
}

void ManualManipulation::initializeBackground()
{
    ui->widget_2->setAutoFillBackground(true);
    QPalette palette = ui->widget->palette();
    palette.setColor(QPalette::Background, Qt::white);
    ui->widget_2->setPalette(palette);
}

void ManualManipulation::initializeButtons()
{
    ui->stackedWidget->setCurrentIndex(0);

    setIoStatus(ui->label1_r1c1_left, 1, ui->label1_r1c1_right, "X42  安全门");
    setIoStatus(ui->label1_r1c2_left, 1, ui->label1_r1c2_right, "X43  安全一");
    setIoStatus(ui->label1_r2c1_left, 1, ui->label1_r2c1_right, "X44  安全二");
    setIoStatus(ui->label1_r2c2_left, 1, ui->label1_r2c2_right, "X45  安全三");
    setIoStatus(ui->label1_r3c1_left, 1, ui->label1_r3c1_right, "X46  安全四");
    setIoStatus(ui->label1_r3c2_left, 1, ui->label1_r3c2_right, "X47  安全五");
    setIoStatus(ui->label1_r4c1_left, 1, ui->label1_r4c1_right, "X48  安全六");
    setIoStatus(ui->label1_r4c2_left, 1, ui->label1_r4c2_right, "X49  安全七");
    setIoStatus(ui->label1_r5c1_left, 1, ui->label1_r5c1_right, "X50  安全八");
    setIoStatus(ui->label1_r5c2_left, 1, ui->label1_r5c2_right, "X51  安全九");
    setIoStatus(ui->label1_r6c1_left, 1, ui->label1_r6c1_right, "X52  安全十");
    setIoStatus(ui->label1_r6c2_left, 1, ui->label1_r6c2_right, "X53  安全一");

    setIoStatus(ui->label2_r1c1_left, 1, ui->label2_r1c1_right, "Y42  安全门");
    setIoStatus(ui->label2_r1c2_left, 1, ui->label2_r1c2_right, "Y43  安全一");
    setIoStatus(ui->label2_r2c1_left, 1, ui->label2_r2c1_right, "Y44  安全二");
    setIoStatus(ui->label2_r2c2_left, 1, ui->label2_r2c2_right, "Y45  安全三");
    setIoStatus(ui->label2_r3c1_left, 1, ui->label2_r3c1_right, "Y46  安全四");
    setIoStatus(ui->label2_r3c2_left, 1, ui->label2_r3c2_right, "Y47  安全五");
    setIoStatus(ui->label2_r4c1_left, 1, ui->label2_r4c1_right, "Y48  安全六");
    setIoStatus(ui->label2_r4c2_left, 1, ui->label2_r4c2_right, "Y49  安全七");
    setIoStatus(ui->label2_r5c1_left, 1, ui->label2_r5c1_right, "Y50  安全八");
    setIoStatus(ui->label2_r5c2_left, 1, ui->label2_r5c2_right, "Y51  安全九");
    setIoStatus(ui->label2_r6c1_left, 1, ui->label2_r6c1_right, "Y52  安全十");
    setIoStatus(ui->label2_r6c2_left, 1, ui->label2_r6c2_right, "Y53  安全一");

    setIoStatus(ui->label3_r1c1_left, 1, ui->label3_r1c1_right, "Y42  抱一开");
    setIoStatus(ui->label3_r1c2_left, 1, ui->label3_r1c2_right, "Y43  抱一开");
    setIoStatus(ui->label3_r2c1_left, 1, ui->label3_r2c1_right, "Y44  抱一开");
    setIoStatus(ui->label3_r2c2_left, 1, ui->label3_r2c2_right, "Y45  抱一开");
    setIoStatus(ui->label3_r3c1_left, 1, ui->label3_r3c1_right, "Y46  抱一开");
    setIoStatus(ui->label3_r3c2_left, 1, ui->label3_r3c2_right, "Y47  抱一开");
    setIoStatus(ui->label3_r4c1_left, 1, ui->label3_r4c1_right, "Y48  抱一开");
    setIoStatus(ui->label3_r4c2_left, 1, ui->label3_r4c2_right, "Y49  抱一开");
    setIoStatus(ui->label3_r5c1_left, 1, ui->label3_r5c1_right, "Y50  抱一开");
    setIoStatus(ui->label3_r5c2_left, 1, ui->label3_r5c2_right, "Y51  抱一开");
    setIoStatus(ui->label3_r6c1_left, 1, ui->label3_r6c1_right, "Y52  抱一开");
    setIoStatus(ui->label3_r6c2_left, 1, ui->label3_r6c2_right, "Y53  抱一开");
}

void ManualManipulation::connectSignalsAndSlots()
{
    connect(ui->pb_nextPage, &QPushButton::clicked, this, [&](){
        if (ui->stackedWidget_2->currentIndex() == 1)
            ui->stackedWidget_2->setCurrentIndex(2);
    });

    connect(ui->pb_nextPage, &QPushButton::clicked, this, [&](){
        if (ui->stackedWidget_2->currentIndex() == 0)
            ui->stackedWidget_2->setCurrentIndex(1);
    });

    connect(ui->pb_lastPage, &QPushButton::clicked, this, [&](){
        if (ui->stackedWidget_2->currentIndex() == 1)
            ui->stackedWidget_2->setCurrentIndex(0);
    });

    connect(ui->pb_lastPage, &QPushButton::clicked, this, [&](){
        if (ui->stackedWidget_2->currentIndex() == 2)
            ui->stackedWidget_2->setCurrentIndex(1);
    });

    connect(ui->pb_axisExpress, &QPushButton::clicked, this, [&](){
        ui->pb_nextPage->hide();
        ui->pb_lastPage->hide();
        if (ui->stackedWidget->currentIndex() == 0)
            ui->stackedWidget->setCurrentIndex(1);
    });

    connect(ui->pb_ioStatus, &QPushButton::clicked, this, [&](){
        ui->pb_nextPage->show();
        ui->pb_lastPage->show();
        if (ui->stackedWidget->currentIndex() == 1)
            ui->stackedWidget->setCurrentIndex(0);
    });

    connect(ui->pb_lastControllerPage, &QPushButton::clicked, this, [&](){
        if (ui->stackedWidget_3->currentIndex() == 1)
            ui->stackedWidget_3->setCurrentIndex(0);
    });

    connect(ui->pb_nextControllerPage, &QPushButton::clicked, this, [&](){
        if (ui->stackedWidget_3->currentIndex() == 0)
            ui->stackedWidget_3->setCurrentIndex(1);
    });

    connect(ui->pb_addSpeed, &QPushButton::clicked, this, [&](){
        emit addSpeedValue();
        ui->speed_flexibleLabel->setText(pendantInterfaceData.speedMutableLabel);
    });

    connect(ui->pb_decSpeed, &QPushButton::clicked, this, [&](){
        emit decSpeedValue();
        ui->speed_flexibleLabel->setText(pendantInterfaceData.speedMutableLabel);
    });

    connect(ui->manualReturnMenu, &QPushButton::clicked, this, &ManualManipulation::pushReturnMenu);
    connect(ui->manualReturnTeachMode, &QPushButton::clicked, this, &ManualManipulation::pushReturnTeachMode);
    connect(ui->manualReturnCaltimer, &QPushButton::clicked, this, &ManualManipulation::pushReturnCalTimer);
    connect(ui->pb_returnZero, &QPushButton::clicked, this, &ManualManipulation::pushReturnZero);
    connect(ui->pb_automatic, &QPushButton::clicked, this, &ManualManipulation::pushReturnAutomatic);

    connect(ui->page_5_r1c1, &QPushButton::clicked, this, [&](){
        emit sendParamValue_TCP_("IoOutput", "EmbraceOpen1","true");
    });
}

void ManualManipulation::setTable(QTableWidget* table)
{
    table->setEditTriggers(QAbstractItemView::NoEditTriggers);
    table->verticalHeader()->setHidden(true);

    table->setColumnCount(7);
    table->setRowCount(5);
    table->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);

    table->setColumnWidth(0,55);
    table->setColumnWidth(1,135);
    table->setColumnWidth(2,45);
    table->setColumnWidth(3,45);
    table->setColumnWidth(4,45);
    table->setColumnWidth(5,45);
    table->setColumnWidth(6,45);

    table->setRowHeight(0,70);
    table->setRowHeight(1,70);
    table->setRowHeight(2,70);
    table->setRowHeight(3,70);
    table->setRowHeight(4,70);

    table->setHorizontalHeaderLabels(QStringList() << "  " << "当前位置/编码器" << "原点" << "极限" << "刹车" << "警报" << "到位");
    QHeaderView* header = table->horizontalHeader();
    QBrush brush(Qt::black);
    for (int i = 0; i < header->count(); i++) {
        QTableWidgetItem *item = new QTableWidgetItem(header->model()->headerData(i, Qt::Horizontal).toString());
        item->setTextColor(brush.color());
        item->setTextAlignment(Qt::AlignVCenter | Qt::AlignLeft);
        table->setHorizontalHeaderItem(i, item);

        QFont font = item->font();
        font.setPointSize(13);
        item->setFont(font);
    }

    QStringList axisName;
    axisName << " Y轴" << " MX轴" << " SX轴" << " MZ轴" << " SX轴";
    for(int row = 0; row < 5; row++)
    {
        QTableWidgetItem* item = new QTableWidgetItem();
        item->setText(axisName[row]);
        item->setForeground(Qt::black);
        item->setBackgroundColor(Qt::white);
        item->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
        ui->table_axisExpress->setItem(row, 0, item);

        QFont font = item->font();
        font.setPointSize(13);
        item->setFont(font);
    }

    for(int row = 0; row < 5; row++){
        setTableCellText(table, row, 1, "136.6 / 57");
    }
    for(int row = 0; row < 5; row++){
        setTableCellColor(table, row, 2, "灰色");
    }
    for(int row = 0; row < 5; row++){
        setTableCellColor(table, row, 3, "红色");
    }
    for(int row = 0; row < 5; row++){
        setTableCellColor(table, row, 4, "绿色");
    }
    for(int row = 0; row < 5; row++){
        setTableCellColor(table, row, 5, "绿色");
    }
    for(int row = 0; row < 5; row++){
        setTableCellColor(table, row, 6, "绿色");
    }
}

void ManualManipulation::updateMantuableData()
{
    setSpeedValue(ui->speed_flexibleLabel);
}

void ManualManipulation::pushReturnMenu()
{
    emit returnMenu();
}

void ManualManipulation::pushReturnTeachMode()
{
    emit returnTeachMode();
}

void ManualManipulation::pushReturnCalTimer()
{
    emit returnCalTimer();
}

void ManualManipulation::pushReturnZero()
{
    emit returnZero();
}

void ManualManipulation::pushReturnAutomatic()
{
    emit returnAutomatic();
}

ManualManipulation::~ManualManipulation()
{
    delete ui;
}


/*
void ManualManipulation::saveButtonStates()
{
    saveButtonState("pb_nextPage", ui->pb_nextPage->isChecked());
    saveButtonState("pb_lastPage", ui->pb_lastPage->isChecked());
}

void ManualManipulation::parseIoState()
{
    tinyxml2::XMLDocument doc;
    if (doc.LoadFile(receivePath) != tinyxml2::XML_SUCCESS) {
        qDebug() << "Error loading XML file.888";
        return;
    }
    tinyxml2::XMLElement* root = doc.RootElement();
    if (!root) {
        qDebug() << "No root element found in XML.";
        return;
    }
    tinyxml2::XMLElement* ioState = root->FirstChildElement("ioState");
    if (!ioState) {
        std::cout << "No ioState element found in XML." << std::endl;
        return;
    }
    tinyxml2::XMLElement* ioStateElement = ioState->FirstChildElement("ioStateElement");
    while (ioStateElement) {
        const char* nameAttribute = ioStateElement->Attribute("Name");
        //qDebug() << nameAttribute;
        ioStateElement = ioStateElement->NextSiblingElement("ioStateElement");
    }
}
*/

void ManualManipulation::displayTechnParam(Techn_Param_ptr tech_param)
{
    if(tech_param->PendantIoParam.X04)
    {
        setIoStatus(ui->label1_r1c1_left, 2, ui->label1_r1c1_right, "X04 安全门");
    }
    else
    {
        setIoStatus(ui->label1_r1c2_left, 1, ui->label1_r1c2_right, "X04 安全门");
    }

    setTableCellText(ui->table_axisExpress, 1, 1, getStringFromTechnParam(tech_param->AxisMX.AxisPos, tech_param->AxisMX.EncodePos));

    if(tech_param->AxisMX.IsZero)
    {
        setTableCellColor(ui->table_axisExpress, 1, 2, "绿色");
    }
    else
    {
        setTableCellColor(ui->table_axisExpress, 1, 2, "红色");
    }
}
